﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WarOfDev.Web.ViewModels;
using WarOfDev.EF;

namespace WarOfDev.Web.Models.MappingToViewModel
{
    public static class UserMapping
    {
        public static void BaseToViewModel(UserViewModel vm, User model)
        {
            vm.Id = model.Id;
            vm.IsLocked = model.IsLocked;
            vm.CreatedOn = model.CreatedOn;
            vm.CreatedBy = model.CreatedBy;
            vm.ModifiedBy = model.ModifiedBy;
            vm.ModifiedOn = model.ModifiedOn;
            vm.UserId = model.UserId;
            vm.UserName = model.UserName;
            vm.Password = model.Password;
            vm.Description = model.Description;

            vm.Roles = string.Join(",", model.Roles.Select(r => r.RoleId).ToArray());
            var group = model.Groups.FirstOrDefault(g => g.UserId == model.Id);
            vm.GroupId = group != null ? group.GroupId : string.Empty;
        }

        public static UserViewModel ToViewModel(this User model)
        {
            UserViewModel vm = new UserViewModel();
            BaseToViewModel(vm, model);
            return vm;
        }
    }
}